package cn.yanjr.system.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.lang3.StringUtils;

import cn.yanjr.common.dao.BaseDaoImpl;
import cn.yanjr.common.dao.JDBCUtils;
import cn.yanjr.system.dao.SysAreaDao;
import cn.yanjr.system.entity.SysArea;

public class SysAreaDaoImpl extends BaseDaoImpl<String, SysArea> implements SysAreaDao{
	
	@Override
	public List<SysArea> findAll(SysArea area) throws SQLException {
		String sql = "select * from sys_area";
		return findEntitys(sql);
	}

	@Override
	public SysArea findOne(String id) throws SQLException {
		String sql = "select * from sys_area where id=?";
		return findFirstEntity(sql, id);
	}

	@Override
	public int save(SysArea area) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(SysArea area) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delete(String id) throws SQLException {
		String sql = "delete from sys_area  where id=? ";
		return update(sql.toString(), id);
	}

	@Override
	public int count(String where) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		StringBuilder sql = new StringBuilder();
		sql
		.append("select count(*) from sys_area where ")
		.append(where);
		Long count = queryRunner.query(sql.toString(), new ScalarHandler<Long>());
		
		return count != null ? count.intValue() : 0;
	}

	@Override
	public List<SysArea> findBySql(String sql) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		List<SysArea> list = null;
		list = queryRunner.query(sql, beanListHandler);
		return list;
	}

	@Override
	public List<SysArea> findByWhere(String where) throws SQLException {
		StringBuilder sb = new StringBuilder();
		sb.append("select * from sys_area where ").append(where);
		return findEntitys(sb.toString());
	}

	@Override
	public List<SysArea> findPage(String where, int page, int limit, String orderBy) throws SQLException {
		StringBuilder sb = new StringBuilder();
		sb.append("select * from sys_area where ").append(where);
		if(StringUtils.isNotEmpty(orderBy)) {
			sb.append(" order by ").append(orderBy);
		}
		sb.append(" limit ").append((page - 1) * limit).append(", ").append(limit);
		return findEntitys(sb.toString());
	}

}
